package com.drinks.yiniang.common.recycler.decoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.drinks.yiniang.App;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * Created by han on 2019/2/16 0016.
 */

public class ItemSpacesDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private int espSpace = 0;
    private int limitItemCount = -1;
    private boolean hasTopSpace = false;
    private boolean hasBottomSpace = false;
    private boolean hasLeftSpace = false;
    private boolean hasRightSpace = false;

    private ItemSpacesDecoration(int space, int espSpace, boolean hasLeftSpace, boolean hasTopSpace, boolean hasRightSpace, boolean hasBottomSpace, int limitCount) {
        this.space = space;
        this.espSpace = espSpace;
        this.hasLeftSpace = hasLeftSpace;
        this.hasTopSpace = hasTopSpace;
        this.hasRightSpace = hasRightSpace;
        this.hasBottomSpace = hasBottomSpace;
        this.limitItemCount = limitCount;
    }

    private ItemSpacesDecoration(int space) {
        this.space = space;
        this.hasTopSpace = true;
        this.hasBottomSpace = false;
        this.hasLeftSpace = false;
        this.hasRightSpace = false;
        this.limitItemCount = -1;
    }

    public static ItemSpacesDecoration getItemPXDecoration(int space) {

        return new ItemSpacesDecoration(space);
    }

    public static ItemSpacesDecoration getItemDPDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space);
    }

    public static ItemSpacesDecoration getItemTopBottomDPDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, false, true, false, true, -1);
    }

    public static ItemSpacesDecoration getLimitItemCountDPDecoration(int space, int limitCount) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, false, false, false, false, limitCount);
    }

    public static ItemSpacesDecoration getItemTopDPDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, false, true, false, false, -1);
    }

    public static ItemSpacesDecoration getItemBottomDPDecoration(int space) {
        return getLimitItemCountBottomDPDecoration(space, -1);
    }

    public static ItemSpacesDecoration getLimitItemCountBottomDPDecoration(int space, int limit) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, false, false, false, true, limit);
    }

    public static ItemSpacesDecoration getLRTItemCountDPDecoration(int space) {

        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, true, true, true, false, -1);
    }

    public static ItemSpacesDecoration getLRBItemCountDPDecoration(int space) {

        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, true, false, true, true, -1);
    }

    public static ItemSpacesDecoration getItemAllDPDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new ItemSpacesDecoration(space, 0, true, true, true, true, -1);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // Add top margin only for the first item to avoid double space between items
        int position = parent.getChildLayoutPosition(view);

        if (hasTopSpace) {
            if (position < limitItemCount) {
                outRect.top = espSpace;
            } else {
                outRect.top = space;
            }
        }

        if (hasBottomSpace) {
            if (position < limitItemCount) {
                outRect.bottom = espSpace;
            } else {
                outRect.bottom = space;
            }
        }

        if (hasLeftSpace && hasRightSpace) {
            if (position < limitItemCount) {
                outRect.left = espSpace;
                outRect.right = espSpace;
            } else {
                outRect.left = space;
                outRect.right = space;
            }
        } else {
            if (hasLeftSpace) {
                if (position < limitItemCount) {
                    outRect.left = espSpace;
                } else {
                    outRect.left = space;
                }
            }

            if (hasRightSpace) {
                if (position < limitItemCount) {
                    outRect.right = espSpace;
                } else {
                    outRect.right = space;
                }
            }
        }
    }
}
